package cn.ac.iscas.action.thesis;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.ac.iscas.service.thesis.IThesisDocService;

import com.opensymphony.xwork2.ActionSupport;


@Controller @Scope("prototype")
public class ThesisDocumentDownload extends ActionSupport{
	/**
	 * 处理文档的下载服务
	 */
	private static final long serialVersionUID = -2937390798778816655L;
	private String fileName;
	private String fileRealName;
	@Resource private IThesisDocService thesisDocumentService;
	
	public void setFileName()
	{
		// 得到请求下载的文件名
		this.fileName = ServletActionContext.getRequest().getParameter("name");
		this.fileRealName = ServletActionContext.getRequest().getParameter("realname");
	}
	public String getFileName() throws UnsupportedEncodingException {
//    	fileName = new String(fileName.getBytes(),"ISO-8859-1");
		fileName = URLDecoder.decode(fileName,"utf-8");
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileRealName()
	{
	return this.fileRealName;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	
	public InputStream getDownloadFile()//函数名需要与struts文件中的result参数一致
	{
		this.setFileName();
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		String filePath = "/documents/";
		System.out.println(realpath);
		//从数据库中找出文档放到Inpustream中
		return ServletActionContext.getServletContext().getResourceAsStream(filePath + fileRealName);
	}
	
	public String execute() throws Exception
	{
		return SUCCESS;
	}
	
	public IThesisDocService getThesisDocumentService() {
		return thesisDocumentService;
	}
	public void setThesisDocumentService(IThesisDocService thesisDocumentService) {
		this.thesisDocumentService = thesisDocumentService;
	}
}
